package util.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * mysql jdbc连接
 *
 * Created by J.Chen on 2017/5/19.
 */
public class MysqlConnect {

    private final static String url = "jdbc:mysql://localhost:%d/%s";

    public static Connection connect(MysqlConf conf) {
        try {
            //1、加载驱动
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2、创建连接
        Connection conn = null;

        try {
            conn = DriverManager.getConnection(String.format(url, conf.getLport(), conf.getRdatabase()), conf.getRuser(), conf.getRpwd());

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}
